package com.dev.base.expand.mvvm;

import com.dev.base.able.IBaseView;
import com.dev.base.utils.AbilityUtils;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.Lifecycle;
import ohos.aafwk.abilityjet.databinding.DataBinding;
import ohos.aafwk.abilityjet.databinding.DataBindingUtil;
import ohos.aafwk.content.Intent;
import ohos.agp.components.ComponentContainer;


public abstract class BaseMVVMSlice<VM extends BaseViewModel, DB extends DataBinding> extends AbilitySlice {
    private ComponentContainer contentContainer;
    private Lifecycle lifecycle;
    private int layout = 0;
    private IBaseView iBaseView;
    protected VM viewModel;
    private DB dataBinding;

    protected boolean ifIsThisSlice;

    public DataBinding getContentDataBinding() {
        return dataBinding;
    }

    public void setiBaseView(IBaseView iBaseView) {
        this.iBaseView = iBaseView;
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        contentContainer = AbilityUtils.getContentContainer(iBaseView.getAbilitySliceContext(), iBaseView.getLayout());
        setUIContent(contentContainer);
        try {
             dataBinding=DataBindingUtil.createBinding(iBaseView.getLayout(), iBaseView.getAbilitySliceContext());
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (dataBinding != null) {
            dataBinding.initComponent(contentContainer);
            lifecycle = new Lifecycle();
            if(viewModel!=null){
                lifecycle.addObserver(viewModel);
            }
            iBaseView.todo(dataBinding);
        }
        ifIsThisSlice=true;

    }
    @Override
    public void onActive() {
        super.onActive();
        ifIsThisSlice=true;
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
        ifIsThisSlice=false;
    }


    protected abstract VM getViewModel();

    @Override
    protected void onInactive() {
        super.onInactive();
    }


}
